#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "TyrInit.h"
//#include "TyrInitXML.h"
//#include <malloc.h>
int useskill(int i, int user, struct skill curskill);
int turn();
//=====================FINDMISSION================
struct mission findmission(char tmpname[S])
{
  int j, k;
  int x;
  int tmp;
  int founded = 0;
  struct mission result;
  j = strlen(tmpname)-1;
  if(tmpname[j] == '\n') 
    tmpname[j] = '\0';
  for (j=0;j<=allM.numM-1;j++)
    {
      x = 0;
      for (k=0;k<=strlen(tmpname)-1;k++)
    if (tolower(tmpname[k]) != tolower(allM.Mpool[j].name[k]))
      x = 1;
      if (x==0)
    {
      printf("Mission founded, mission\'s name = \"%s\"\n", allM.Mpool[j].name);
      tmp = j;
      founded++;
    }
    }
  if (founded == 0)
    {
      printf("Error - failed to found desired mission, starting over!\n");
      printf("Re-enter mission name\n");
      scanf("%s", tmpname);
      result = findmission(tmpname);
    }
  if (founded == 1)
    {
      printf("Testing with mission \"%s\"\n", allM.Mpool[tmp].name);
      result = allM.Mpool[tmp];
    }
  if (founded > 1)
    {
      //free(consoletmp);
      printf("More then one mission has the name like that, starting over\n");
      printf("Re-enter mission name\n");
      scanf("%s", tmpname);
      result = findmission(tmpname);
    }
  return result;
}
//=====================SHUFFLE===================
int shuffle()
{
  int j, i, n;
  int tmp;
  struct card tmpdecks[D][2];
  for (n=0;n<=D;n++)
    for (i=0;i<=1;i++)
      for (j=0;j<=cleft[i]-1;j++)
    {
      tmp = rand() % cleft[i];
      tmpdecks[j][i] = decks[j][i];
      decks[j][i] = decks[tmp][i];
      decks[tmp][i]=tmpdecks[j][i];
    }
  if (disablelogs[0] != 'y')
    printf ("Deck shuffled, now decks are like that:\n");
  for (i=0;i<=1;i++)
    {
      if (i==0)
    if (disablelogs[0] != 'y')
      printf ("Deck D:\n");
      if (i==1)
    if (disablelogs[0] != 'y')
      printf ("Deck A:\n");
      for (j=0;j<=cleft[i]-1;j++)
    if (disablelogs[0] != 'y')
      printf("%s\n", decks[j][i].name);
    }
  return 1;
}
//=======================COMMANDERSTDIN===============================
int commanderstdin()
{
  int founded = 0;
  int j, k, x, dummy;
  printf("Enter beginning of desired commander\'s name>  ");
  scanf("%s", consoletmp);
  j = strlen(consoletmp)-1;
  if(consoletmp[j] == '\n') 
    consoletmp[j] = '\0';
  for (j=0;j<=allcards.numcomms-1;j++)
    {
      x = 0;
      for (k=0;k<=strlen(consoletmp)-1;k++)
    if (tolower(consoletmp[k]) != tolower(allcards.commpool[j].name[k]))
      x = 1;
      if (x==0)
    {
      printf("Commander founded, commander name = \"%s\"\n\n", allcards.commpool[j].name);
      customcomm = allcards.commpool[j];
      founded = 1;
    }
    }
  if (founded == 0)
    {
      printf("Error - failed to found desired commander, starting over!\n");
      dummy = commanderstdin();
    }
  return 1;
}
//=======================CARDSTDIN=================================
int cardstdin(int i)
{
  //malloc(S*sizeof(char));
  //char *consoletmp = malloc(S*sizeof(char));
  int founded = 0;
  int k, x, dummy;
  int j = 0;
  int numtoadd = 0;
  printf("Enter desired card for your deck(%d already in it)>  ", i);
  scanf("%s", consoletmp);
  j = strlen(consoletmp)-1;
  if(consoletmp[j] == '\n') 
    consoletmp[j] = '\0';
  for (j=0;j<=allcards.numcards-1;j++)
    {
      x = 0;
      //printf("===DEBUG====, checking card named %s from deck\n", allcards.cardpool[j].name);
      for (k=0;k<=strlen(consoletmp)-1;k++)
    if (tolower(consoletmp[k]) != tolower(allcards.cardpool[j].name[k]))
      x = 1;
      if (x==0)
    {
      printf("Card founded, cards name = \"%s\"\n", allcards.cardpool[j].name);
      dummy = j;
      founded++;
    }
    }
  if (founded == 0)
    {
      //      free(consoletmp);
      printf("Error - failed to found desired card, starting over!\n");
      numtoadd = cardstdin(i);
    }
  if (founded == 1)
    {
      printf("How many copies of this card will your deck have(0 for cancel)?>  ");
      scanf("%d", &numtoadd);
      if (numtoadd == 0)
    {
      printf("Ok, starting over\n");
      numtoadd = cardstdin(i);
    } else
    for (j=i;j<=i+numtoadd-1;j++)
      {
        if (j<=customnumcards-1)
          {
        printf("Card named \"%s\" added to deck\n", allcards.cardpool[dummy].name);
        customdeck[j] = allcards.cardpool[dummy];
          }
        if (j>customnumcards-1)
          {
        printf("Too many cards! Scrapping last one...\n");
                numtoadd--;
          }
      }
      //free(consoletmp);
    }
  if (founded > 1)
    {
      //free(consoletmp);
      printf("More then one card has the name like that, starting over\n");
      numtoadd = cardstdin(i);
    }
  return numtoadd;
}
//========================ADD CARD FROM LIBRARY============================
//===========INITMISSION===============
int initmission(char tmpstring[30])
{
  int founded = 0;
  int k, x, dummy;
  int j = 0;
  j = strlen(tmpstring)-1;  //
  if(tmpstring[j] == '\n')  //not used  
    tmpstring[j] = '\0';    //
  for (j=0;j<=allcards.numcards-1;j++)
    {
      x = 0;
      for (k=0;k<=strlen(tmpstring)-1;k++)
    if (tolower(tmpstring[k]) != tolower(allcards.cardpool[j].name[k]))
      x = 1;
      if (x==0)
    {
      dummy = j;
      founded++;
    }
    }
  if (founded == 0)
    printf("Error \"0\" in initializing mission deck %d!\n", allM.numM);
  if (founded == 1)
    {
      printf("Card named \"%s\" added to missiondeck %d\n", allcards.cardpool[dummy].name, allM.numM);
      allM.Mpool[allM.numM].deck[allM.Mpool[allM.numM].numcards] = allcards.cardpool[dummy];               
      allM.Mpool[allM.numM].numcards++;
    }
  if (founded > 1)
    printf("Error \"Many\" in initializing mission deck %d!\n", allM.numM);
  return 1;
}

//===========INITSAMPLE===============
int initsample(int dnum, char tmpstring[30])
{
  int founded = 0;
  int k, x, dummy;
  int j = 0;
  j = strlen(tmpstring)-1;
  if(tmpstring[j] == '\n')  //not used  
    tmpstring[j] = '\0';    //
  for (j=0;j<=allcards.numcards-1;j++)
    {
      x = 0;
      for (k=0;k<=strlen(tmpstring)-1;k++)
    if (tolower(tmpstring[k]) != tolower(allcards.cardpool[j].name[k]))
      x = 1;
      if (x==0)
    {
      dummy = j;
      founded++;
    }
    }
  if (founded == 0)
    printf("Error \"0\" in initializing sample deck %d!\n", dnum);
  if (founded == 1)
    {
      printf("Card named \"%s\" added to sampledeck\n", allcards.cardpool[dummy].name);
      sampledecks[samplenumcards[dnum]][dnum] = allcards.cardpool[dummy];
      samplenumcards[dnum]++;
    }
  if (founded > 1)
    printf("Error \"Many\" in initializing sample deck %d!\n", dnum);
  return 1;
}
//========================CUSTOMSTDIN==================================
int customstdin()
{
  int dummy;
  char answer[S];
  char answer2[S];
  char tmpname[S];
  int numadded, j;
  answer[0] = 'y';
  if (whichtest[0] == 's')
    {
      printf("You can test any of the pre-made decks or you can make your own\n");
      printf ("Do you want to make custom deck(y/n)?>  ");
      fgets(answer, S*sizeof(char), stdin);
    }
  if (whichtest[0] == 'm')
    {
      printf("Enter mission name (number or DR4 for displaced riders 4, etc)>  ");
      fgets(tmpname, S*sizeof(char), stdin);
      curmission = findmission(tmpname);
      //printf("DEBUG, %d\n", curmission.numcards);
    }
  if (answer[0] == 'y')
    {
      printf("Do you want to look through library(y/n)?>  ");
      scanf("%s", answer2);
      if (answer2[0] == 'y')
    {
      printf("Do you want to see all commander\'s list(y/n)?>  ");
      scanf("%s", answer2);
      if (answer2[0] == 'y')
        for (j=0;j<=allcards.numcomms-1;j++)
          printf("%s\n", allcards.commpool[j].name);
      printf("Do you want to see all card\'s list(y/n)?>  ");
      scanf("%s", answer2);
      if (answer2[0] == 'y')
        for (j=0;j<=allcards.numcards-1;j++)
          printf("%s(%d), %d/%d/%d, skills %s %s %s\n", allcards.cardpool[j].name, j, allcards.cardpool[j].attack, allcards.cardpool[j].defense, allcards.cardpool[j].cd, allcards.cardpool[j].skills[0].which, allcards.cardpool[j].skills[1].which, allcards.cardpool[j].skills[2].which);
    }
      printf("Okay, lets begin.\n");
      numadded = commanderstdin();
      printf("Enter amount of cards in your deck(excluding commander)>  ");
      scanf("%d", &customnumcards);
      printf("You don\'t need to enter full card\'s name, 5-6 letters will be enough\n");
      printf("No unique/legendary check still!\n");
      for (j=0;j<=customnumcards-1;j++)
    {
      numadded = cardstdin(j);
      if (numadded > 0)
        j = j+numadded-1;
    }
      printf("Enter name of your team>  ");
      //fgets(customname, 30*sizeof(char), stdin);
      scanf("%s", customname);
      dummy = getchar();
      j = strlen(customname)-1;
     if(customname[j] == '\n') 
    customname[j] = '\0';
      custom = 1;
    }
  if (custom == 1)
    {
      printf("Okay, now your deck looks like this:\n");
      for (j=0;j<=customnumcards-1;j++)
    printf("%s\n", customdeck[j].name);
    }
  return 1;
}
//============SMALLINIT=================
int smallinit()
{
  int j, dummy;
  int tmpA = -1;
  int tmpD = -1;
  cleft[0] = 10;
  cleft[1] = 10;
  if (custom == 1 && testattack == 1)
    cleft[1] = customnumcards;
  if (custom == 1 && testattack == 0)
    cleft[0] = customnumcards;
  //-----------Initializing sample decks for next test--------------
  if (whichtest[0] == 's')
    {
      if (testattack == 1)
    {
      tmpD = testnumber;
      cleft[0] = samplenumcards[testnumber];
    }
      if (testattack == 0)
    {
      tmpA = testnumber;
      cleft[1] = samplenumcards[testnumber];
    }
      if (custom == 0 && testattack == 1)
    {
      tmpA = decknumber;
      cleft[1] = samplenumcards[decknumber];
    }
      if (custom == 0 && testattack == 0)
    {
      tmpD = decknumber;
      cleft[0] = samplenumcards[decknumber];
    }
      if (tmpD > -1)
    {
      for (j=0;j<=cleft[0]-1;j++)
        decks[j][0] = sampledecks[j][tmpD];
      comms[0] = samplecomms[tmpD];
    }
      if (tmpA > -1)
    {
      for (j=0;j<=cleft[1]-1;j++)
        decks[j][1] = sampledecks[j][tmpA];
      comms[1] = samplecomms[tmpA];
    }
    }
  //------------------Initializing mission deck for next test---------------
  if (whichtest[0] == 'm')
    {
      tmpD = 0; //defending deck is not custom :)
      cleft[0] = curmission.numcards;
      for (j=0;j<=cleft[0]-1;j++)
    decks[j][0] = curmission.deck[j];
      comms[0] = curmission.missioncomm;
    }
  //---------------------------
  if (tmpA == -1)//attacking deck is custom
    {
      for (j=0;j<=customnumcards;j++)
    decks[j][1] = customdeck[j];
      comms[1] = customcomm;
    }
  if (tmpD == -1)//defending deck is custom
    {
      for (j=0;j<=customnumcards;j++)
    decks[j][0] = customdeck[j];
      comms[0] = customcomm;
    }
  if (disablelogs[0] != 'y')
    printf("\nDecks initialised, currently in deck A - %d cards, in deck D - %d cards\n", cleft[1], cleft[0]);
  if (disablelogs[0] != 'y')
    printf("Commander of deck A - %s, commander of deck D - %s\n", comms[1].name, comms[0].name);
  dummy=shuffle();
  numturn = 1;
  ended = 0;
  return 1;
}
//=============ENDGAME=====================
int endgame(int i)
{
  if (whichtest[0] == 's' && AIgoing == 0)
    wins[i][surge][testnumber]++;
  if (whichtest[0] == 'm' && AIgoing == 0)
    winsM[i]++;
  if (AIgoing == 1 && i == 1)
    curwinsAI++;
  if (AIgoing == 0)
    numround++;
  int j, k;
  char tmpname[S];
  //strcmp(tmpname, "                                     ");
  if (i == 0)
    {
      //for (j=0;j<=namelength[testnumber]-1;j++)
      //  tmpname[j]=teamname[testnumber][j];
      if (disablelogs[0] != 'y')      
    printf("Defender wins!\n");
      //printf("Team %s wins!\n", tmpname);
      numturn = 51;
    }
  if (i == 1)
    {
      //for (j=0;j<=namelength[decknumber]-1;j++)
      //  tmpname[j]=teamname[decknumber][j];
      if (disablelogs[0] != 'y')
    printf("Attacker wins!\n");
      //printf("Team %s wins!\n", tmpname);
      numturn = 51;
    }
  cleft[0] = 0;
  cleft[1] = 0;
  bfield.numunit[0] = 0;
  bfield.numunit[1] = 0;
  bfield.numstr[0] = 0;
  bfield.numstr[1] = 0;
  return 1;
}

//============TRYTOREGENERATE===============
int trytoregenerate (struct card curcard)
{
  int reg=0;
  int try=0;
  int j;
  //printf("DEBUG, %d %d %d\n", curcard.skills[0].power, curcard.skills[1].power, curcard.skills[2].power);
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "regenerate") == 0)
      {
    reg = curcard.skills[j].power;
    //printf("DEBUG, reg = %d\n", reg);
      }
  if (reg != 0)
    {
      try = rand() % 2;
      if (try == 1)
    if (disablelogs[0] != 'y')
      printf("Card %s regenerated, now it has %d defense\n", curcard.name, reg);
      if (try == 0)
    {    
      if (disablelogs[0] != 'y')
        printf("Regeneration attempt of card %s had failed\n", curcard.name);
      reg = 0;
    }
    }    
  return reg;
}
//==================TRYTOEVADE==================//returns 1 if evade suceeded
int trytoevade (struct card curcard)
{
  int evade=0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "evade") == 0)
    evade = 1;
  if (evade != 0)
    evade = rand() % 2;
  return evade;
}
//==========FINDIFPAYBACK=======
int findifpayback (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "payback") == 0)
      x=1;
  return x;
}
//=================NUMACTUNITS===================
int numactunits (int i, int user, char faction)//user is for units not to rally before them
{
  int j;
  int x=0;
  if (user>0)
    user--;
  for (j=user;j<=bfield.numunit[i]-1;j++)
    if (bfield.units[j][i].jam == 0 && bfield.units[j][i].immob == 0 && bfield.units[j][i].curhp > 0)
      {
    if (faction == 'a')
      if (bfield.units[j][i].curcd == 0)
        x++;
    if (faction != 'a')
      if (bfield.units[j][i].curcd == 0 && bfield.units[j][i].faction == faction)
        x++;
      }
  if (disablelogs[0] != 'y')
    printf("Checking number of active units, result - %d\n", x);
  return x;
}
//=================NUMWEAKABLE=================
int numweakable (int i, char faction)
{
  int j;
  int x=0;
  for (j=0;j<=bfield.numunit[i]-1;j++)
    if (bfield.units[j][i].jam == 0 && bfield.units[j][i].immob == 0 && bfield.units[j][i].curhp > 0 && bfield.units[j][i].curatt > 0 && bfield.units[j][i].curcd == 0)
      {
    if (faction == 'a')
        x++;
    if (faction != 'a')
      if (bfield.units[j][i].faction == faction)
        x++;
      }
  if (disablelogs[0] != 'y')
    printf("Checking number of targets, result - %d\n", x);
  return x;
}
//===============NUMHEALABLE===================
int numhealable (int i, char faction)
{
  int j;
  int x=0;
  for (j=0;j<=bfield.numunit[i]-1;j++)
    if (bfield.units[j][i].curhp > 0 && bfield.units[j][i].curhp < bfield.units[j][i].defense)
      {
    if (faction == 'a')
        x++;
    if (faction != 'a')
      if (bfield.units[j][i].faction == faction)
        x++;
      }
  if (disablelogs[0] != 'y')
    printf("Checking number of wounded units, result - %d\n", x);
  return x;
}
//==================FINDACTUNIT==================
findactunit(int i, int user, int num, char faction)
{
  int j;
  int a=0;
  int x=user;
  if (x>0)
    x--;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.units[x][i].immob == 0 && bfield.units[x][i].jam == 0 && bfield.units[x][i].curhp > 0)
    {
      if (faction == 'a')
        if (bfield.units[x][i].curcd == 0)
          a++;
      if (faction != 'a')
        if (bfield.units[x][i].curcd == 0 && bfield.units[x][i].faction == faction)
          a++;
    }
      if (a<j)
    x++;
    }
  return x;
}
//=======================FINDWEAKABLE=========================
findweakable(int i, int num, char faction)
{
  int j;
  int a=0;
  int x=0;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.units[x][i].immob == 0 && bfield.units[x][i].jam == 0 && bfield.units[x][i].curhp > 0 && bfield.units[x][i].curatt > 0)
    {
      if (faction == 'a')
        if (bfield.units[x][i].curcd == 0)
          a++;
      if (faction != 'a')
        if (bfield.units[x][i].curcd == 0 && bfield.units[x][i].faction == faction)
          a++;
    }
      if (a<j)
    x++;
    }
  return x;
}
//=================RALLYONE===================
int rallyone(int i, int target, int power)
{
  bfield.units[target][i].curatt = bfield.units[target][i].curatt + power;
  if (disablelogs[0] != 'y')
    printf("Rally! Unit %s(%d) has now attack %d\n", bfield.units[target][i].name, target, bfield.units[target][i].curatt);
  return 1;
}
//================WEAKENONE===================
int weakenone(int i, int user, int proc, int target, int power)
{
  int x;
  int payback = findifpayback(bfield.units[target][i]);
  int dummy;
  int flip = rand() % 2;
  x = trytoevade(bfield.units[target][i]);
  if (x==0){
    bfield.units[target][i].curatt = bfield.units[target][i].curatt - power;
    if (disablelogs[0] != 'y')    
      printf("Weaken! Unit %s(%d) has now attack %d\n", bfield.units[target][i].name, target, bfield.units[target][i].curatt);
    if (proc == 0 && user!=0 && payback == 1 && flip == 1)
      {
    if (disablelogs[0] != 'y')
      printf("Payback! Weaken have been paybacked!\n");
    dummy = weakenone(1-i, target+1, 1, user-1, power);
      }
    if (proc == 0 && user != 0 && payback == 1 && flip == 0)
      if (disablelogs[0] != 'y')
    printf("Payback failed!\n");
  }
  if (x==1){
    if (disablelogs[0] != 'y')    
      printf("Weaken was evaded by unit %s(%d)!\n", bfield.units[target][i].name, target);
  }
  return 1;
}
//=================HEALONE====================
int healone(int i, int target, int power)
{
  int tmp;
  int noheal = 0;
  if (bfield.units[target][i].curhp == bfield.units[target][i].defense)
    {
      if (disablelogs[0] != 'y')
    printf("Unit %s(%d) is already on full hp!\n", bfield.units[target][i].name, target);
      noheal = 1;
    }
  if (bfield.units[target][i].curhp > 0)
    {
      bfield.units[target][i].curhp = bfield.units[target][i].curhp + power;
      if (bfield.units[target][i].curhp > bfield.units[target][i].defense)
    bfield.units[target][i].curhp = bfield.units[target][i].defense;
      if (noheal == 0)
    if (disablelogs[0] != 'y')    
      printf("Heal! Unit %s(%d) has now %d hp\n", bfield.units[target][i].name, target, bfield.units[target][i].curhp);
    } 
  return 1;
}

//==================FINDALIVEUNIT==================
findaliveunit(int i, int num, char faction)
{
  int j;
  int a=0;
  int x=0;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.units[x][i].curhp > 0)
    {
      if (faction == 'a')
        a++;
      if (faction != 'a')
        if (bfield.units[x][i].faction == faction)
          a++;
    }
      if (a<j)
    x++;
    }
  return x;
}
//====================FINDNONJAM===============
findnonjam(int i, int num, char faction)
{
  int j;
  int a=0;
  int x=0;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.units[x][i].curhp > 0 && bfield.units[x][i].curcd == 0 && bfield.units[x][i].jam == 0)
    {
      if (faction == 'a')
        a++;
      if (faction != 'a')
        if (bfield.units[x][i].faction == faction)
          a++;
    }
      if (a<j)
    x++;
    }
  return x;
}
//===============FINDALIVESTR===================
findalivestr(int i, int num)
{
  int j;
  int a=0;
  int x=0;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.structures[x][i].curhp > 0)
    a++;
      if (a<j)
    x++;
    }
  return x;
}
//=================FINDHEALABLE=====================
findhealable(int i, int num, char faction)
{
  int j;
  int a=0;
  int x=0;
  j = (rand() % num)+1;
  while (a<j)
    {
      if (bfield.units[x][i].curhp > 0 && bfield.units[x][i].curhp < bfield.units[x][i].defense)
    {
      if (faction == 'a')
        a++;
      if (faction != 'a')
        if (bfield.units[x][i].faction == faction);
          a++;
    }
      if (a<j)
    x++;
    }
  return x;
}
//=============NUMALIVEUNITS===================
int numaliveunits(int i, char faction)
{
  int j;
  int x=0;
  for (j=0;j<=bfield.numunit[i]-1;j++)
    {
      if (faction == 'a')
    if (bfield.units[j][i].curhp > 0)
      x++;
      if (faction != 'a')
    if (bfield.units[j][i].curhp > 0 && bfield.units[j][i].faction == faction)
      x++;
    }
  if (disablelogs[0] != 'y')
    printf("Checking number of alive units, result - %d\n", x);
  return x;
}
//================NUMNONJAM====================
int numnonjam(int i, char faction)
{
  int j;
  int x=0;
  for (j=0;j<=bfield.numunit[i]-1;j++)
    {
      if (faction == 'a')
    if (bfield.units[j][i].curhp > 0 && bfield.units[j][i].jam == 0 && bfield.units[j][i].curcd == 0)
      x++;
      if (faction != 'a')
    if (bfield.units[j][i].curhp > 0 && bfield.units[j][i].faction == faction && bfield.units[j][i].jam == 0 && bfield.units[j][i].curcd == 0)
      x++;
    }
  if (disablelogs[0] != 'y')
    printf("Checking number of targets, result - %d\n", x);
  return x;
}
//===============NUMALIVESTR=================
int numalivestr(int i)
{
  int j;
  int x=0;
  for (j=0;j<=bfield.numstr[i]-1;j++)
    if (bfield.structures[j][i].curhp > 0)
      x++;
  if (disablelogs[0] != 'y')
    printf("Checking number of targets, result - %d\n", x);
  return x;
}
//================STRIKEONE====================
int strikeone (int i, int user, int proc, int target, int power)//payback -
{
  int x;
  int dummy;
  int payback = findifpayback(bfield.units[target][i]);
  int flip = rand() % 2;
  int reg;
  x = trytoevade(bfield.units[target][i]);
  if (x==0) {
    bfield.units[target][i].curhp = bfield.units[target][i].curhp - power - bfield.units[target][i].enfeeble;
    if (bfield.units[target][i].enfeeble > 0)
      if (disablelogs[0] != 'y')
    printf("Unit %s suffers additional %d damage from enfeeble\n", bfield.units[target][i].name, bfield.units[target][i].enfeeble);
    if (bfield.units[target][i].curhp > 0)
      if (disablelogs[0] != 'y')
    printf("Strike(%d)! Unit %s(%d) has now %d hp\n", power, bfield.units[target][i].name, target, bfield.units[target][i].curhp);
    if (bfield.units[target][i].curhp <= 0)
      {
    if (disablelogs[0] != 'y')
      printf("Strike(%d)! Unit %s(%d) died from strike.\n", power, bfield.units[target][i].name, target);
    reg = trytoregenerate(bfield.units[target][i]);
    if (reg != 0)
      bfield.units[target][i].curhp = reg;
      }
    if (proc == 0 && user != 0 && payback == 1 && flip == 1)
      {
    if (disablelogs[0] != 'y')
      printf("Payback! Strike have been paybacked!\n");
    dummy = strikeone(1-i, target+1, 1, user-1, power);
      }
    if (proc == 0 && user != 0 && payback == 1 && flip == 0)
      if (disablelogs[0] != 'y')
    printf("Payback failed!\n");
    } else
    if (disablelogs[0] != 'y')
      printf("Unit %s(%d) evades strike!\n", bfield.units[target][i].name, target);
  return 1;
}
//================MIMICONE====================
int mimicone (int i, int user, int target, int power)
{
  int x;
  int j;
  struct skill tmpskill;
  x = trytoevade(bfield.units[target][i]);
  if (x==0) {
    if (disablelogs[0] != 'y')
      printf("Mimic! Unit %s(%d) is mimicked\n", bfield.units[target][i].name, target);
    for (j=0;j<=2;j++)
      {
    if ((strcmp(bfield.units[target][i].skills[j].which, "mimic") != 0) && (strcmp(bfield.units[target][i].skills[j].which, "mimicall") != 0))
    strcpy(tmpskill.which, bfield.units[target][i].skills[j].which);
    tmpskill.power = bfield.units[target][i].skills[j].power;
    tmpskill.target = 'a';
    useskill(1-i, user, tmpskill);
      }
  }
  if (x==1) 
    if (disablelogs[0] != 'y')
      printf("Unit %s(%d) evades mimic!\n", bfield.units[target][i].name, target);
  return 1;
}
//=============ENFEEBLEONE==================
int enfeebleone (int i, int user, int proc, int target, int power)//payback -
{
  int x; //x=1 => skill evaded
  int dummy;
  int payback = findifpayback(bfield.units[target][i]);
  int flip = rand() % 2;
  x = trytoevade(bfield.units[target][i]);
  if (x==0) {
    bfield.units[target][i].enfeeble = bfield.units[target][i].enfeeble + power;
    if (bfield.units[target][i].curhp > 0)
      if (disablelogs[0] != 'y')
    printf("Enfeeble! Each attack on unit %s(%d) will deal %d more damage this turn\n", bfield.units[target][i].name, target, bfield.units[target][i].enfeeble);
    if (proc == 0 && user!=0 && payback == 1 && flip == 1)
      {
    if (disablelogs[0] != 'y')
      printf("Payback! Enfeeble have been paybacked!\n");
    dummy = enfeebleone(1-i, target+1, 1, user-1, power);
      }
    if (proc == 0 && user != 0 && payback == 1 && flip == 0)
      if (disablelogs[0] != 'y')
    printf("Payback failed!\n");
  }
  if (x==1) 
    if (disablelogs[0] != 'y')
      printf("Unit %s(%d) evades enfeeble!\n", bfield.units[target][i].name, target);
  return 1;
}
//=================JAMONE=====================
int jamone (int i, int user, int proc, int target)//payback -
{
  int x;
  int dummy;
  int flip = rand() % 2;
  int payback = 0;
  payback = findifpayback(bfield.units[target][i]);
  x = trytoevade(bfield.units[target][i]);
  if (x==0) {
    bfield.units[target][i].jam = 1;
    if (disablelogs[0] != 'y')
      printf("Jam! Unit %s(%d) is jammed.\n", bfield.units[target][i].name, target);
    x = trytoevade(bfield.units[user-1][1-i]);
    if (proc == 0 && user!=0 && payback == 1 && flip == 1)
      {
    if (disablelogs[0] != 'y')
      printf("Payback! Jam have been paybacked!\n");
    dummy = jamone(1-i, target+1, 1, user-1);
      }
    if (proc == 0 && user != 0 && payback == 1 && flip == 0)
      if (disablelogs[0] != 'y')
    printf("Payback failed!\n");
  } else 
    if (disablelogs[0] != 'y')
      printf("Unit %s(%d) evades jamming!\n", bfield.units[target][i].name, target);
  return 1;
}
//================SIEGEONE=====================
int siegeone (int i, int target, int power)//evade -
{
  int x;
  int reg;
  x = trytoevade(bfield.structures[target][i]);
  if (x==0)
    {
      bfield.structures[target][i].curhp = bfield.structures[target][i].curhp - power;
      if (bfield.structures[target][i].curhp > 0)
    if (disablelogs[0] != 'y')
      printf("Siege(%d)! Structure %s(%d) has now %d hp\n", power, bfield.structures[target][i].name, target, bfield.structures[target][i].curhp);
      if (bfield.structures[target][i].curhp <= 0)
    {
      if (disablelogs[0] != 'y')
        printf("Siege! Structure %s(%d) is destroyed.\n", bfield.structures[target][i].name, target);
      reg = trytoregenerate(bfield.structures[target][i]);
      if (reg != 0)
        bfield.structures[target][1].curhp = reg;
    }
    } else {
    if (disablelogs[0] != 'y')
      printf("Structure %s(%d) evades siege!\n", bfield.units[target][i].name, target);
  }
  return 1;
}
//================USESKILL=====================
int useskill(int i, int user, struct skill curskill)//Rally, strike, heal, weaken works fine
{
  int maxrandom;
  int curtarget;
  int dummy;
  int j, tryjam;
  char tmp[N];
  if (strcmp(curskill.which, "rally") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Rally activates...\n");
      maxrandom = numactunits (i, user, curskill.target);
      if (maxrandom > 0)
    {
      curtarget = findactunit(i, user, maxrandom, curskill.target);
      dummy = rallyone (i, curtarget, curskill.power);
    }
    }
  if (strcmp(curskill.which, "weaken") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Weaken activates...\n");
      maxrandom = numweakable (1-i, curskill.target);
      if (maxrandom > 0)
        {
          curtarget = findweakable(1-i, maxrandom, curskill.target);
          dummy = weakenone (1-i, user, 0, curtarget, curskill.power);
        }
    }
  if (strcmp(curskill.which, "strike") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Strike is coming...\n");
      maxrandom = numaliveunits (1-i, curskill.target);
      if (maxrandom > 0)
    {
      curtarget = findaliveunit (1-i, maxrandom, curskill.target);
      dummy = strikeone (1-i, user, 0, curtarget, curskill.power);
    }
    }
  if (strcmp(curskill.which, "enfeeble") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Enfeeble is coming...\n");
      maxrandom = numaliveunits (1-i, curskill.target);
      if (maxrandom > 0)
    {
      curtarget = findaliveunit (1-i, maxrandom, curskill.target);
      dummy = enfeebleone (1-i, user, 0, curtarget, curskill.power);
    }
    }
  if (strcmp(curskill.which, "mimic") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Mimicry activated...\n");
      maxrandom = numaliveunits (1-i, curskill.target);
      if (maxrandom > 0)
    {
      curtarget = findaliveunit (1-i, maxrandom, curskill.target);
      dummy = mimicone (1-i, user, curtarget, curskill.power);
    }
    }
  if (strcmp(curskill.which, "jam") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Jamming...\n");
      tryjam = rand() % 2;
      if (tryjam == 0)
    if (disablelogs[0] != 'y')
      printf("Jam attempt failed!\n");
      if (tryjam == 1)
    {
      maxrandom = numnonjam (1-i, curskill.target);
      if (maxrandom > 0)
        {
          curtarget = findnonjam (1-i, maxrandom, curskill.target);
          dummy = jamone (1-i, user, 0, curtarget);
        }
    }
    }
  if (strcmp(curskill.which, "siege") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Siege is planned...\n");
      maxrandom = numalivestr (1-i);//-
      if (maxrandom > 0)
    {
      curtarget = findalivestr (1-i, maxrandom);//-
      dummy = siegeone (1-i, curtarget, curskill.power);//-
    }
    }
  
  if (strcmp(curskill.which, "heal") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Healing wounded...\n");
      maxrandom = numhealable (i, curskill.target);
      if (maxrandom > 0)
    {
      curtarget = findhealable (i, maxrandom, curskill.target);
      dummy = healone (i, curtarget, curskill.power);
    }
    }
  if (strcmp(curskill.which, "healall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Healing all...\n");
      if (curskill.target = 'a')
    for (j=0;j<=bfield.numunit[i]-1;j++)
      healone(i, j, curskill.power);
      if (curskill.target != 'a')
    for (j=0;j<=bfield.numunit[i]-1;j++)
      if (bfield.units[j][i].faction == curskill.target)
        healone(i, j, curskill.power);
    }
  if (strcmp(curskill.which, "rallyall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Rallying all...\n");
      if (user > 0)
    user--;
      if (curskill.target == 'a')
    for (j=user;j<=bfield.numunit[i]-1;j++)
      if (bfield.units[j][i].immob == 0 && bfield.units[j][i].jam == 0 && bfield.units[j][i].curhp > 0 && bfield.units[j][i].curcd == 0) 
        rallyone(i, j, curskill.power);
      if (curskill.target != 'a')
    for (j=user;j<=bfield.numunit[i]-1;j++)
      if (bfield.units[j][i].immob == 0 && bfield.units[j][i].jam == 0 && bfield.units[j][i].curhp > 0 && bfield.units[j][i].curcd == 0 && bfield.units[j][i].faction == curskill.target) 
        rallyone(i, j, curskill.power);
    }
  if (strcmp(curskill.which, "weakenall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Weakening all...\n");
      if (curskill.target == 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][1-i].immob == 0 && bfield.units[j][1-i].jam == 0 && bfield.units[j][1-i].curatt > 0 && bfield.units[j][1-i].curhp > 0 && bfield.units[j][1-i].curcd == 0)
        weakenone(1-i, user, 0, j, curskill.power);
      if (curskill.target != 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][1-i].immob == 0 && bfield.units[j][1-i].jam == 0 && bfield.units[j][1-i].curatt > 0 && bfield.units[j][1-i].curhp > 0 && bfield.units[j][1-i].curcd == 0 && bfield.units[j][1-i].faction == curskill.target)
        weakenone(1-i, user, 0, j, curskill.power);
    }
  if (strcmp(curskill.which, "strikeall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Masstrike is coming!...\n");
      if (curskill.target == 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][1-i].curhp > 0)
        strikeone(1-i, user, 0, j, curskill.power);
      if (curskill.target != 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][1-i].curhp > 0 && bfield.units[j][1-i].faction == curskill.target)
        strikeone(1-i, user, 0, j, curskill.power);
    }
  if (strcmp(curskill.which, "siegeall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Massiege is planned!...\n");
      for (j=0;j<=bfield.numstr[1-i]-1;j++)
    if (bfield.structures[j][1-i].curhp > 0)
      siegeone(1-i, j, curskill.power);
    }
  if (strcmp(curskill.which, "enfeebleall") == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Massenfeeble is coming!...\n");
      if (curskill.target == 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][i].curhp > 0)
        enfeebleone(1-i, user, 0, j, curskill.power);
      if (curskill.target != 'a')
    for (j=0;j<=bfield.numunit[1-i]-1;j++)
      if (bfield.units[j][i].curhp > 0 && bfield.units[j][1-i].faction == curskill.target)
        enfeebleone(1-i, user, 0, j, curskill.power);
    }
  return 1;
}
//====================ACTION_CARD===========
int useactioncard(int i, struct card curcard)
{
  int j = 0;
  if (disablelogs[0] != 'y')
    printf("Using actioncard %s...\n", curcard.name);
  j=useskill (i, 0, curcard.skills[0]);
  j=useskill (i, 0, curcard.skills[1]);
  j=useskill (i, 0, curcard.skills[2]);
  return 1;
}
//==================NEWCARD============
int newcard(int i)
{
  int j = 0;
  if (cleft[i] > 0)
    {
      cleft[i]--;
      if (decks[cleft[i]][i].type == 'u')//adding unit
    {
      if (disablelogs[0] != 'y')
        printf("Adding unit %s, current number of units - %d\n", decks[cleft[i]][i].name, bfield.numunit[i]+1);
      bfield.units[bfield.numunit[i]][i] = decks[cleft[i]][i];//put unit on field
      bfield.numunit[i]++;
    }
      if (decks[cleft[i]][i].type == 's')//adding structure
    {
      if (disablelogs[0] != 'y')
        printf("Adding structure %s, current number of structures - %d\n", decks[cleft[i]][i].name, bfield.numstr[i]+1);
      bfield.structures[bfield.numstr[i]][i] = decks[cleft[i]][i];//put unit on field
      bfield.numstr[i]++;
    }
      if (decks[cleft[i]][i].type == 'a')//playing action card
    j = useactioncard(i, decks[cleft[i]][i]);
    }
  return 1;
}
//================COMMANDER'S_TURN========
int commturn(int i)
{
  int dummy;
  if (disablelogs[0] != 'y')
    printf("Commander %s is acting\n", comms[i].name);
  dummy=useskill (i, 0, comms[i].skills[0]);
  dummy=useskill (i, 0, comms[i].skills[1]);
  dummy=useskill (i, 0, comms[i].skills[2]);
}
//===========POISONDMG==========
int poisondmg(int i)
{
  int j;
  int dummy;
  int reg;
  for (j=0;j<=bfield.numunit[i]-1;j++)
    if (bfield.units[j][i].poison > 0)
      {
    if (disablelogs[0] != 'y')
      printf("Unit %s(%d) suffers from poison -%d\n", bfield.units[j][i].name, j, bfield.units[j][i].poison);
    bfield.units[j][i].curhp = bfield.units[j][i].curhp - bfield.units[j][i].poison;
    if (bfield.units[j][i].curhp <= 0)
      {    
        if (disablelogs[0] != 'y')        
          printf("Unit %s(%d) died from poison\n", bfield.units[j][i].name, j);
        reg = trytoregenerate(bfield.units[j][i]);
        if (reg != 0)
          bfield.units[j][i].curhp = reg;
      }
      }
}
//============STRUCTTURN==========
int structturn(int i)
{
  int j;
  for (j=0;j<=bfield.numstr[i]-1;j++)
    if (bfield.structures[j][i].curcd == 0)//structure is active
      {
    useskill(i, 0, bfield.structures[j][i].skills[0]);
    useskill(i, 0, bfield.structures[j][i].skills[1]);
    useskill(i, 0, bfield.structures[j][i].skills[2]);
      }
  return 1;
}
//===========FINDIFFLYING==========
int findifflying (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "flying") == 0)
      x=1;
  return x;
}
//=============FINDIFFEAR===========
int findiffear (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "fear") == 0)
      x=1;
  return x;
}
//==========FINDIFCOUNTER===========
int findifcounter (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "counter") == 0)
      x=curcard.skills[j].power;
  return x;
}
//==========FINDIFLEECH============
int findifleech (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "leech") == 0)
      x = curcard.skills[j].power;
  return x;
}
//==========FINDIFVALOR============
int findifvalor (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "valor") == 0)
      x = curcard.skills[j].power;
  return x;
}
//=========FINDIFCRUSH=============
int findifcrush (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "crush") == 0)
      x = curcard.skills[j].power;
  return x;
}
//==========FINDIFSWIPE============
int findifswipe (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "swipe") == 0)
      x = 1;
  return x;
}
//==========FINDIFSIPHON============
int findifsiphon (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "siphon") == 0)
      x = curcard.skills[j].power;
  return x;
}
//============FINDIFAA============
int findifaa (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "antiair") == 0)
      x = curcard.skills[j].power;
  return x;
}

//===========FINDIFPOISON========
int findifpoison (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "poison") == 0)
      x = curcard.skills[j].power;
  return x;
}
//===========FINDIFIMMOB=========
int findifimmob (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "immobilize") == 0)
      x = 1;
  return x;
}
//=========FINDIFFLURRY===========
int findifflurry (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "flurry") == 0)
      x = curcard.skills[j].power;
  return x;
}
//==========FINDIFARMOR===========
int findifarmor (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "armor") == 0)
      x = curcard.skills[j].power;
  return x;
}
//==========FINDIFPIERCE========
int findifpierce (struct card curcard)
{
  int x = 0;
  int j;
  for (j=0;j<=K-1;j++)
    if (strcmp(curcard.skills[j].which, "pierce") == 0)
      x = curcard.skills[j].power;
  return x;
}
//===========FINDWALL============
int findwall (int i)
{
  int x = 0;
  int j, k;
  for (j=bfield.numstr[i]-1;j>=0;j--)
    for (k=0;k<=K-1;k++)
      if (strcmp(bfield.structures[j][i].skills[k].which, "wall") == 0 && bfield.structures[j][i].curhp > 0)
    x = j+1;
  return x;
}
//==========DEALCRUSHDAMAGE========
int dealcrushdamage (int i, int crush)
{
  int wall;
  int dummy;
  int reg;
  wall = findwall(1-i);
  if (wall > 0)
    {
      bfield.structures[wall-1][1-i].curhp = bfield.structures[wall-1][1-i].curhp - crush;
      if (disablelogs[0] != 'y')
    printf("Crush deals %d damage to enemy wall %s(%d hp left)\n", crush, bfield.structures[wall-1][1-i].name, bfield.structures[wall-1][1-i].curhp);
      if (bfield.structures[wall-1][1-i].curhp <= 0)
    {
      if (disablelogs[0] != 'y')
        printf("The wall %s is destroyed\n", bfield.structures[wall-1][1-i].name);
      reg = trytoregenerate(bfield.structures[wall-1][1-i]);
      if (reg != 0)
        bfield.structures[wall-1][1-i].curhp = reg;
    }
    }
  if (wall == 0)
    {
      if (disablelogs[0] != 'y')
    printf("Crush deals %d damage to enemy commander(%d hp left)\n", crush, comms[1-i].curhp-crush);
      comms[1-i].curhp = comms[1-i].curhp - crush;
      if (comms[1-i].curhp<=0 && ended == 0)
    {
      dummy = endgame(i);
      ended = 1;
    }
    }
}
//=============ATTACK============//swipe, crush, etc are missing
int attack(int i, int num, int proc, int ifswipe)
{
  int swipe = 0;//1 if swipe, swipe+fear = fail
  int dam = 0; //attack damage
  int flyatt = 0; //1 - flying
  int flydef = 0;
  int aa = 0; //>0 if att unit has anti-air
  int miss = 0;//miss == 1 - attack failed
  int leech = 0;//hp leeched
  int siphon = 0;//hp siphoned
  int valor = 0;
  int crush = 0;
  int reg = 0;//>0 if unit should be regenerated
  int poison = 0;//poison afflicted
  int immobilize = 0;//1 - attacker has immobilize
  int wall = 0;//!=0 means damage is dealt to wall
  int counter = 0;//amount of damage countered
  int armor = 0;//decreace damage
  int pierce = 0;//negate armor
  int flurry = 0;//flurry+swipe = ?
  int repeat = 0;
  int fear = 0;//1 - ignore opposing unit
  int dummy;
  int flip, j;
  int valortmp1, valortmp2;
  if (bfield.units[num][i].curhp <= 0)
    if (disablelogs[0] != 'y')
      printf("Unit %s cannot attack, because it is dead as a rock.\n", bfield.units[num][i].name);
  if (bfield.units[num][i].curhp > 0)
    {
      fear = findiffear(bfield.units[num][i]);
      if (fear == 1)
    if (disablelogs[0] != 'y')
      printf("Fear! Attacking commander directly!\n");
      swipe = findifswipe(bfield.units[num][i]);
      if (swipe == 1)
    if (ifswipe == 0)
      if (bfield.units[num][1-i].curhp > 0 && bfield.numunit[1-i] >= num+1)//swipe will proc
        {
          if (disablelogs[0] != 'y')
        printf("Swipe! Attacking adjacent units!\n");
          if (num > 0)
        if (bfield.units[num-1][1-i].curhp > 0)
            dummy = attack(i, num, proc, -2);
          if (bfield.numunit[1-i] >= num+2)
        if (bfield.units[num+1][1-i].curhp > 0)
          dummy = attack(i, num, proc, -1);
        else {
          swipe = 0;
        } else {
        swipe = 0;
          }
        } else{
        swipe = 0;
      }
      if (bfield.numunit[1-i] >= num+1 && bfield.units[num+ifswipe+swipe][1-i].curhp > 0 && fear == 0)//attacking unit
    {
      dam = bfield.units[num][i].curatt;
      armor = findifarmor(bfield.units[num+ifswipe+swipe][1-i]);
      pierce = findifpierce(bfield.units[num][i]);
      flurry = findifflurry(bfield.units[num][i]);
      flydef = findifflying(bfield.units[num+ifswipe+swipe][1-i]);
      flyatt = findifflying(bfield.units[num][i]);
      aa = findifaa(bfield.units[num+ifswipe][i]);
      leech = findifleech(bfield.units[num][i]);
      siphon = findifsiphon(bfield.units[num][i]);
      valor = findifvalor(bfield.units[num][i]);
      crush = findifcrush(bfield.units[num][i]);
      poison = findifpoison(bfield.units[num][i]);
      immobilize = findifimmob(bfield.units[num][i]);
      counter = findifcounter(bfield.units[num+ifswipe+swipe][1-i]);
      flip = rand() % 2;
      if (flurry > 0 && proc == 0)
        {
          if (flip == 0)
        if (disablelogs[0] != 'y')
        printf ("Flurry failed!\n");
          if (flip == 1)
        {
          if (disablelogs[0] != 'y')
            printf("Flurry! Unit %s attacks %d more times!\n", bfield.units[num][i].name, flurry);
          repeat = flurry;
        }
      }
      if (flydef == 1 && aa == 0 && flyatt == 0)
        miss = rand() % 2;
      if (flydef == 1 && aa > 0)
        {
          dam = dam + aa;
          if (disablelogs[0] != 'y')
          printf("Anti-air! Damage is increased by %d!\n", aa);
        }
      if (miss == 0 && dam > 0)//counting damage
        {
          if (valor>0)
        {
          valortmp1 = numaliveunits(i, 'a');
          valortmp2 = numaliveunits(1-i, 'a');
          if(valortmp1>=valortmp2)
            valor = 0;
          if (valor>0)
            if (disablelogs[0] != 'y')
              printf("Valor! Unit %s(%d) damage is increased by %d\n", bfield.units[num][i].name, num, valor);
          dam = dam + valor;
        }
          dam = dam + bfield.units[num+ifswipe+swipe][1-i].enfeeble;
          if (bfield.units[num+ifswipe+swipe][1-i].enfeeble > 0)
        if (disablelogs[0] != 'y')
          printf("Enfeeble raised damage on unit %s(%d) by %d\n", bfield.units[num+ifswipe+swipe][1-i].name, num, bfield.units[num+ifswipe+swipe][1-i].enfeeble);
          if (pierce > 0 && armor > 0)
        {
          armor = armor-pierce;
          if (armor < 0)
            armor = 0;
        if (disablelogs[0] != 'y')
          printf("Pierce! Unit %s armor is lowered by %d(%d remaining)\n", bfield.units[num+ifswipe+swipe][1-i].name , pierce, armor);
        }
          if (armor > 0)
        {
          dam = dam-armor;
          if (dam<0)
            dam = 0;
          if (disablelogs[0] != 'y')
            printf("Armor! Unit %s damage is lowered by %d(%d remaining)\n", bfield.units[num][i].name, armor, dam);
        }
        }
    if (miss == 0 && dam > 0)//damage-dependant skills
      {
        bfield.units[num+ifswipe+swipe][1-i].curhp = bfield.units[num+ifswipe+swipe][1-i].curhp - dam;
        if (disablelogs[0] != 'y')
          printf ("Unit %s(%d) damages unit %s(%d) for %d damage(%d hp left)\n", bfield.units[num][i].name, num, bfield.units[num+ifswipe+swipe][1-i].name, num+ifswipe+swipe, dam, bfield.units[num+ifswipe+swipe][1-i].curhp);
        if (bfield.units[num+ifswipe+swipe][1-i].curhp <= 0)//kill+, crush?
          {
        if (disablelogs[0] != 'y')
          printf("Unit %s(%d) died from the attack\n", bfield.units[num+ifswipe+swipe][1-i].name, num);
        if (crush>0)
          {
            if (disablelogs[0] != 'y')
              printf("CRUSH!11 Huge boooom from unit\'s %s destruction deals damage to commander/walls or something like that.\n", bfield.units[num+ifswipe+swipe][1-i].name);
            dummy = dealcrushdamage(i, crush);
          }
        reg = trytoregenerate(bfield.units[num+ifswipe+swipe][1-i]);
        if (reg != 0)
          bfield.units[num+ifswipe+swipe][1-i].curhp = reg;
          }
        if (bfield.units[num+ifswipe+swipe][1-i].curhp > 0 && poison > bfield.units[num+ifswipe+swipe][1-i].poison )
          {
        if (disablelogs[0] != 'y')
          printf("Unit %s(%d) is poisoned(%d)!\n", bfield.units[num+ifswipe+swipe][1-i].name, num, poison);
        bfield.units[num+ifswipe+swipe][1-i].poison = poison;
          }
        if (bfield.units[num+ifswipe+swipe][1-i].curhp > 0 && immobilize == 1)
          {
        immobilize = rand() % 2;
        if (immobilize == 0)          
          if (disablelogs[0] != 'y')
            printf("Unit %s(%d) is failed to immobilize %s!\n", bfield.units[num][i].name, num, bfield.units[num+ifswipe+swipe][1-i].name);
        if (immobilize == 1)
          {
            bfield.units[num+ifswipe+swipe][1-i].immob = 1;
            if (disablelogs[0] != 'y')
              printf("Unit %s(%d) is immobilized!\n", bfield.units[num+ifswipe+swipe][1-i].name, num);
          }
          }
        if (counter > 0)
          {
        bfield.units[num][i].curhp = bfield.units[num][i].curhp - counter;
        if (disablelogs[0] != 'y')
          printf("Unit %s counters for %d damage (%d hp left)\n", bfield.units[num+ifswipe+swipe][1-i].name, counter, bfield.units[num][i].curhp);
          }
        if (leech > 0 && bfield.units[num][i].curhp > 0)
          {
        if (leech > dam)
          leech = dam;
        bfield.units[num][i].curhp = bfield.units[num][i].curhp+leech;
        if (bfield.units[num][i].curhp > bfield.units[num][i].defense)
          bfield.units[num][i].curhp = bfield.units[num][i].defense;
        if (disablelogs[0] != 'y')
          printf("Unit %s leeched +%d(now %d) hp\n", bfield.units[num][i].name, leech, bfield.units[num][i].curhp);
          }
        if (siphon > 0)
          {
        if (siphon > dam)
          siphon = dam;
        comms[i].curhp = comms[i].curhp+siphon;
        if (comms[i].curhp > comms[i].health)
          comms[i].curhp = comms[i].health;
        if (disablelogs[0] != 'y')
          printf("Unit %s siphoned +%d hp(now commander has %d hp)\n", bfield.units[num][i].name, siphon, comms[i].curhp);
          }
      }
    if (miss == 1)
      if (disablelogs[0] != 'y')
        printf("Attack from unit %s(%d) missed\n", bfield.units[num][i].name, num);
      } 
    else //attacking commander
      {
    wall = findwall(1-i);
    if (wall > 0)
      {
        bfield.structures[wall-1][1-i].curhp = bfield.structures[wall-1][1-i].curhp - bfield.units[num][i].curatt;
        if (disablelogs[0] != 'y')
          printf("Unit %s(%d) deals %d damage to enemy wall %s(%d hp left)\n", bfield.units[num][i].name, num, bfield.units[num][i].curatt, bfield.structures[wall-1][1-i].name, bfield.structures[wall-1][1-i].curhp);
        if (bfield.structures[wall-1][1-i].curhp <= 0)
          {
        if (disablelogs[0] != 'y')
          printf("The wall %s is destroyed\n", bfield.structures[wall-1][1-i].name);
        reg = trytoregenerate(bfield.structures[wall-1][1-i]);
        if (reg != 0)
          bfield.structures[wall-1][1-i].curhp = reg;
          }
        counter = findifcounter(bfield.structures[wall-1][1-i]);
        if (counter>0)
          {
        if (disablelogs[0] != 'y')
          printf ("Wall %s counters for %d damage(%d hp left)\n", bfield.structures[wall-1][1-i].name, counter, bfield.units[num][i].curhp-counter);
        bfield.units[num][i].curhp = bfield.units[num][i].curhp-counter;
          }
      }
    if (wall == 0)
      {
        if (disablelogs[0] != 'y')
          printf("Unit %s(%d) deals %d damage to enemy commander(%d hp left)\n", bfield.units[num][i].name, num, bfield.units[num][i].curatt, comms[1-i].curhp-bfield.units[num][i].curatt);
        comms[1-i].curhp = comms[1-i].curhp - bfield.units[num][i].curatt;
        if (comms[1-i].curhp<=0 && ended == 0)
          {
        dummy = endgame(i);
        ended = 1;
          }
      }
      }
  }
  if (ifswipe == 0)
    for (j=0;j<=repeat-1;j++)
      dummy = attack(i, num, 1, ifswipe);
  return 1;
}
//============UNITTURN===========
int unitturn(int i)
{ 
  int j=0;
  int dummy;
  while (j<=bfield.numunit[i]-1 && ended == 0)
    {
      if (bfield.units[j][i].curcd == 0 && bfield.units[j][i].jam == 0 && bfield.units[j][i].curhp > 0)
    {
      dummy = useskill(i, j+1, bfield.units[j][i].skills[0]);
      dummy = useskill(i, j+1, bfield.units[j][i].skills[1]);
      dummy = useskill(i, j+1, bfield.units[j][i].skills[2]);
      if (bfield.units[j][i].immob == 0 && bfield.units[j][i].curhp > 0 && bfield.units[j][i].curatt>0)
        dummy = attack(i, j, 0, 0);
    }
      j++;
    }
  return 1;
}
//==========BURYTHEDEAD==========
int burythedead()
{
  int i;
  int j;
  int k;
  for (i = 0;i<=1;i++)
    for (j=bfield.numunit[i]-1; j>=0; j--)
      if (bfield.units[j][i].curhp<= 0)
    {
      if (disablelogs[0] != 'y')
        printf("Unit %s(%d) buried. R.I.P. ", bfield.units[j][i].name, j);
      if (disablelogs[0] != 'y')
        printf("Units left - %d\n", bfield.numunit[i]-1);
      for (k=j; k<=bfield.numunit[i]-2;k++)
        bfield.units[k][i] = bfield.units[k+1][i];
      bfield.numunit[i]--;
    }
  for (i = 0;i<=1;i++)
    for (j=bfield.numstr[i]-1; j>=0; j--)
      if (bfield.structures[j][i].curhp<= 0)
    {
      if (disablelogs[0] != 'y')
        printf("Place, where was structure %s(%d) is clean now\n", bfield.structures[j][i].name, j);
      if (disablelogs[0] != 'y')
        printf("Structures left - %d\n", bfield.numstr[i]-1);
      for (k=j; k<=bfield.numstr[i]-2;k++)
        bfield.structures[k][i] = bfield.structures[k+1][i];
      bfield.numstr[i]--;
    }
  
  return 1;
}
//============ENDTURN============
int endturn(int i)
{
  int dummy;
  int j;
  dummy = burythedead();
  for (j=0; j<=bfield.numunit[i]-1; j++)
    {
      bfield.units[j][i].curatt = bfield.units[j][i].attack;
      bfield.units[j][i].jam = 0;
      bfield.units[j][i].immob = 0;
      bfield.units[j][1-i].enfeeble = 0;
      if (bfield.units[j][i].curcd > 0)
    bfield.units[j][i].curcd = bfield.units[j][i].curcd - 1;
    }
  for (j=0;j<=bfield.numstr[i]-1;j++)
    if (bfield.structures[j][i].curcd > 0)
      bfield.structures[j][i].curcd = bfield.structures[j][i].curcd - 1;
}
//=====================================AI===================================
int swaphand(int i)//swap cards within hand, playing card No i+?
{
  struct card tmpcard;
  int j;
  tmpcard = decks[i][1];
  for (j=i;j<=cleft[1]-1;j++)
    decks[j][1] = decks[j+1][1];
  decks[cleft[1]-1][1] = tmpcard;
  if (disablelogs[0] != 'y')
    {
      printf("Cards in hand are swapped(%d->%d)!\n", i, cleft[1]-1);
      for (j=0;j<=cleft[1]-1;j++)
    printf("%s\n", decks[j][1].name);
    }
  if (disablelogs[0] != 'y')
    printf("Card %s is first now!\n", tmpcard.name);
  return 1;
}
//==========================SAVE=======================
int saveall()//making backup for decks/field+?
{
  int j, k, l;
  for (j=0;j<=1;j++)
    for(k=0;k<=D-1;k++)
      AIdecks[k][j] = decks[k][j];
  AIcleft[1] = cleft[1];
  AIcleft[0] = cleft[0];
  for (j=0;j<=1;j++)
    for(k=0;k<=bfield.numunit[j]-1;k++)
      AIfield.units[k][j] = bfield.units[k][j];
  for (j=0;j<=1;j++)
    for(k=0;k<=bfield.numstr[j]-1;k++)
      AIfield.structures[k][j] = bfield.structures[k][j];
  AIfield.numunit[1] = bfield.numunit[1];
  AIfield.numunit[0] = bfield.numunit[0];
  AIfield.numstr[1] = bfield.numstr[1];
  AIfield.numstr[0] = bfield.numstr[0];
  AInumturn = numturn;
  AInumround = numround;
  AIcomms[0] = comms[0];
  AIcomms[1] = comms[1];
  if (disablelogs[0] != 'y')
    printf("Position saved!\n");
}
//==========================LOAD=======================
int loadall()//loading deck/field from backup
{
  int j, k, l;
  for (j=0;j<=1;j++)
    for(k=0;k<=D-1;k++)
      decks[k][j] = AIdecks[k][j];
  cleft[1] = AIcleft[1];
  cleft[0] = AIcleft[0];
  for (j=0;j<=1;j++)
    for(k=0;k<=bfield.numunit[j]-1;k++)
      bfield.units[k][j] = AIfield.units[k][j];
  for (j=0;j<=1;j++)
    for(k=0;k<=bfield.numstr[j]-1;k++)
      bfield.structures[k][j] = AIfield.structures[k][j];
  bfield.numunit[1] = AIfield.numunit[1];
  bfield.numunit[0] = AIfield.numunit[0];
  bfield.numstr[1] = AIfield.numstr[1];
  bfield.numstr[0] = AIfield.numstr[0];
  numturn = AInumturn;
  numround = AInumround;
  comms[0] = AIcomms[0];
  comms[1] = AIcomms[1];
  if (disablelogs[0] != 'y')
    printf("Position loaded!\n");
}
//=======================CHECK 1 ROUND ON AI===================
int checkround(int i)
{
  int dummy;
  char tmp[30];
  dummy = swaphand(cleft[1]-1-i);
  //====================1 ROUND SAMPLEDECKS TESTING AI CHECK===============
  if (whichtest[0] == 's')
    {
      while (numturn <= 50)
    {
      if (disablelogs[0] != 'y')
        printf("\n**AI** turn number %d\n", numturn);
      dummy = turn();
      numturn++;
    }
      if (ended == 0)
    dummy = endgame(0); //draw, defense wins
      if (DEBUG == 'y')
    {
      printf ("To exit debug mode type 'x'>  ");
      scanf("%s", tmp);
      printf("==================================================\n\n");
      if (tmp[0] == 'x')
        DEBUG = 'n';
    }
    }
  //=====================1 ROUND MISSIONS TESTING AI CHECK====================
  if (whichtest[0] == 'm')
    {
      while (numturn <= 50)
    {
      if (disablelogs[0] !=  'y')
        printf("\n**AI** turn number %d\n", numturn);
      dummy = turn();
      numturn++;
    }
      if (ended == 0)
    dummy = endgame(0); //draw, defense wins
      if (DEBUG == 'y')
    {
      printf ("To exit debug mode type 'x'>  ");
      scanf("%s", tmp);
      printf("==================================================\n\n");
      if (tmp[0] == 'x')
        DEBUG = 'n';
    }
    }
  loadall();
  ended = 0;
}
//===============================AI RUN===================
int AIrun()
{
  int dummy;
  int j;
  dummy = saveall();
  for (j=0;j<=AIstr-1;j++)
    dummy = checkround(0);
  AIwins[0] = curwinsAI;
  curwinsAI = 0;
  for (j=0;j<=AIstr-1;j++)
    dummy = checkround(1);
  AIwins[1] = curwinsAI;
  curwinsAI = 0;
  if (cleft[1] > 2)
    for (j=0;j<=AIstr-1;j++)
      dummy = checkround(2);
  AIwins[2] = curwinsAI;
  curwinsAI = 0;
  if (disablelogs[0] != 'y')
    printf("Carn %s(%d) wins %d times, card %s(%d) wins %d times, card %s(%d) wins %d times\n", decks[cleft[1]-1][1].name, cleft[1]-1, AIwins[0], decks[cleft[1]-2][1].name, cleft[1]-2, AIwins[1], decks[cleft[1]-3][1].name, cleft[1]-3, AIwins[2]);
  if (AIwins[1]>=AIwins[0] && AIwins[1]>=AIwins[2])//Indian code rulezzz
    {
      if (disablelogs[0] !='y')
    printf("Card %s(%d) is one of the best variants!\n", decks[cleft[1]-2][1].name, cleft[1]-2);
      swaphand(cleft[1]-2);
      saveall();
    }
  if (AIwins[2]>=AIwins[0] && AIwins[2]>AIwins[1])
    {
      if (disablelogs[0] !='y')
    printf("Card %s(%d) is one of the best variants!\n", decks[cleft[1]-3][1].name, cleft[1]-3);
      swaphand(cleft[1]-3);
      saveall();
    }
}
//============TURN===============
int turn()
{
  int dummy;
  int i;
  int j;
  int x;
  i = (numturn+surge) % 2;//0 for def, 1 for att
  dummy = poisondmg(i);
  if (i == 1 && cleft[1]>1 && AIstr > 0 && AIgoing == 0)//AI start
    {
      if (disablelogs[0] != 'y')    
    printf("Starting AI...\n");
      AIgoing = 1;
      for (j=0;j<=2;j++)
    AIwins[j] = 0;
      dummy = AIrun();
      AIgoing = 0;
    }
  dummy = newcard(i);
  dummy = commturn(i);
  dummy = structturn(i);
  dummy = unitturn(i);
  dummy = endturn(i);
  return 1;
}
//==============RESULT==============
int resultprint()
{
  int j, k;
  int tmp1, tmp2;
  double rating = 0.0;
  char tmpnameA[S];
  char tmpnameD[S];
  strcpy(tmpnameA, "                        "); 
  strcpy(tmpnameD, "                        "); 
  printf("\nResults of test:\n");
  //============RESULTPRINT FOR TEST AGAINST SAMPLE DECKS=============
  if(whichtest[0] == 's')
    {
      printf("=============FIGHTING=============\n");
      if (custom == 0 && testattack == 1)
    for (k=0;k<=namelength[decknumber]-1;k++)
      tmpnameA[k]=teamname[decknumber][k];
      if (custom == 0 && testattack == 0)
    for (k=0;k<=namelength[decknumber]-1;k++)
      tmpnameD[k]=teamname[decknumber][k];
      if (custom == 1 && testattack == 1)
    strcpy(tmpnameA, customname);
      if (custom == 1 && testattack == 0)
    strcpy(tmpnameD, customname);
      for (j=0;j<=T-1;j++)
    {
      if (testattack == 1)
        {
          strcpy(tmpnameD, "                        ");
          for (k=0;k<=namelength[j]-1;k++)
        tmpnameD[k]=teamname[j][k];
        }
      if (testattack == 0)
        {
          strcpy(tmpnameA, "                        ");
          for (k=0;k<=namelength[j]-1;k++)
        tmpnameA[k]=teamname[j][k];
        }
      printf("Team %s wins %d times, team %s wins %d times\n", tmpnameA, wins[1][0][j], tmpnameD, wins[0][0][j]);
    }
      for (j=0;j<=T-1;j++)
    rating = rating + wins[testattack][0][j];
      rating = rating / M / T;
      printf("Deck overall rating for fighting - %f\n", rating);
      rating = 0.0;
      printf("=============SURGING==============\n");
      for (j=0;j<=T-1;j++)
    {
      if (testattack == 1)
        {
          strcpy(tmpnameD, "                     ");
          for (k=0;k<=namelength[j]-1;k++)
        tmpnameD[k]=teamname[j][k];
        }
      if (testattack == 0)
        {
          strcpy(tmpnameA, "                     ");
          for (k=0;k<=namelength[j]-1;k++)
        tmpnameA[k]=teamname[j][k];
        }
      printf("Team %s wins %d times, team %s wins %d times\n", tmpnameA, wins[1][1][j], tmpnameD, wins[0][1][j]);
    }
      for (j=0;j<=T-1;j++)
    rating = rating + wins[testattack][1][j];
      rating = rating / M / T;
      printf("Deck overall rating for surging - %f\n", rating);
    }
  //====================RESULTPRINT FOR MISSIONS TEST===============
  if (whichtest[0] == 'm')
    {
      strcpy(tmpnameA, customname);
      strcpy(tmpnameD, curmission.name);
      printf("Team %s wins %d times and loses %d times\n", tmpnameA, winsM[1], winsM[0]);
    }
  return 1;
}
//===========================RE-RUN===============================
//===========MODIFY==========
int modifydeck()
{
  char answer[30];
  int intanswer;
  int dummy;
  int j, numadded, x, tmp, k;
  char consoletmp[30];
  int founded = 0;
  printf("Custom deck looks like this:\n");
  for (j=0;j<=customnumcards-1;j++)
    printf("%s\n", customdeck[j].name);
  printf("Commander - %s\n", customcomm.name);
  printf("Nofcards = %d\n", customnumcards);
  printf("Do you want to Add/Remove card/change Commander/Empty your deck/\n");
  printf("switch between attack/defense Test mode/Rename/\n");
  //printf("change aI strength/");
  printf("change Number of rounds/Nothing(a/r/c/e/t/R/N/n)?>  ");
  scanf ("%s", answer);
  if (answer[0] == 'c')
    {
      dummy = commanderstdin();
      modifydeck();
    }
  if (answer[0] == 'N')
    {
      printf("Enter new number of rounds>  ");
      scanf("%d", &M);
      dummy = getchar();
      modifydeck();
    }
  if (answer[0] == 'i')
    {
      printf("Enter new AI strength>  ");
      scanf("%d", &AIstr);
      dummy = getchar();
      modifydeck();
    }
  if (answer[0] == 'R')
    {
      printf("Enter new name of your team>  ");
      scanf("%s", customname);
      modifydeck();
    }
  if (answer[0] == 'e')
    {
      customnumcards = 0;
      modifydeck();
    }
  if (answer[0] == 't')
    {
      if (whichtest[0] == 's')
    {
      testattack = 1-testattack;
      if (testattack == 0)
        printf("==========Now playing on defense===========\n");
      if (testattack == 1)
        printf("==========Now playing on attack============\n");
    }
      else
    printf("Failed to change test mode!\n");
      modifydeck();
    }
  if (answer[0] == 'a')
    {
      printf("Enter number of cards to add(0-%d)", D-customnumcards);
      scanf(" %d", &intanswer);
      dummy = getchar();//ololobug
      if (intanswer == 0)
    {
      printf("Canceling...\n");
      dummy = modifydeck();
    }
      if (intanswer > 0)
    {
      customnumcards = customnumcards+intanswer;
      for (j=customnumcards-intanswer;j<=customnumcards-1;j++)
        {
          numadded = cardstdin(j);
          if (numadded > 0)
        j = j+numadded-1;
        }
      dummy = modifydeck();
    }
    }
  if (answer[0] == 'r')
    {
      printf("Enter name of card to remove>  ");
      scanf("%s", consoletmp);
      for (j=0;j<=customnumcards-1;j++)
    {
      x = 0;
      for (k=0;k<=strlen(consoletmp)-1;k++)
        if (tolower(consoletmp[k]) != tolower(customdeck[j].name[k]))
          x = 1;
      if (x==0)
        {
          printf("Card to remove founded, card\'s name = \"%s\"\n", customdeck[j].name);
          tmp = j;
          founded++;
        }
    }
      if (founded == 0)
    {
      printf("Error - failed to found desired card, starting over!\n");
      dummy = modifydeck();
    }
      if (founded >= 1)
    {
      printf("Removing last founded card \"%s\"\n", customdeck[tmp].name);
      for (j=tmp;j<=customnumcards-2;j++)
        customdeck[j] = customdeck[j+1];
      customnumcards--;
      dummy = modifydeck();
    }
    }
  return 1;
}
//=========BATTLE===========
int battle()
{
  int dummy;
  int j, k, l;
  char tmp[30];
  for (j=0;j<=1;j++)
    for(k=0;k<=1;k++)
      for(l=0;l<=T-1;l++)
    wins[j][k][l] = 0;
  testnumber = 0;
  winsM[0] = 0;
  winsM[1] = 0;
  //====================SAMPLEDECKS TESTING MAIN===============
  if (whichtest[0] == 's')
    {
      while (testnumber<=T-1)//Fighting
    {
      surge = 0;
      for (k=1; k<=M; k++)
        {
          dummy = smallinit();
          while (numturn <= 50)
        {
          if (disablelogs[0] != 'y')
            printf("\nturn number %d\n", numturn);
          dummy = turn();
          numturn++;
        }
          if (ended == 0)
        dummy = endgame(0); //draw, defense wins
        }
      if (DEBUG == 'y')
        {
          printf ("To exit debug mode type 'x'>  ");
          scanf("%s", tmp);
          if (tmp[0] == 'x')
        DEBUG = 'n';
        }
      testnumber++;
    }
      testnumber = 0;
      while (testnumber<=T-1)//Surging
    {
      surge = 1;
      for (k=1; k<=M; k++)
        {
          dummy = smallinit();
          while (numturn <= 50)
        {
          if (disablelogs[0] !=  'y')
            printf("\nturn number %d\n", numturn);
          dummy = turn();
          numturn++;
        }
          if (ended == 0)
        dummy = endgame(0); //draw, defense wins
        }
      if (DEBUG == 'y')
        {
          printf ("To exit debug mode type 'x'>  ");
          scanf("%s", tmp);
          if (tmp[0] == 'x')
        DEBUG = 'n';
          dummy = getchar();
        }
      testnumber++;
    }
      dummy = resultprint();
    }
  //========================MISSIONS TESTING MAIN====================
  if (whichtest[0] == 'm')
    {
      for (k=0; k<=M-1; k++)
    {
      //printf("DEBUG, %d\n", curmission.numcards);
      dummy = smallinit();
      //printf("DEBUG, %d\n", curmission.numcards);
      while (numturn <= 50)
        {
          if (disablelogs[0] !=  'y')
        printf("\nturn number %d\n", numturn);
          dummy = turn();
          numturn++;
        }
      if (ended == 0)
        dummy = endgame(0); //draw, defense wins
      if (DEBUG == 'y')
        {
          printf ("To exit debug mode type 'x'>  ");
          scanf("%s", tmp);
          if (tmp[0] == 'x')
        DEBUG = 'n';
        }
    }
      dummy = resultprint();
    }
  if (custom == 1)
    {
      printf("Do you want to modify custom deck/etc and try again(y/n)?>  ");//!
      scanf("%s", tmp);
      if (tmp[0] != 'y')
    printf("OK, bye\n");
      if(tmp[0] == 'y')
    {
      dummy = modifydeck();
      printf("Okay, now your deck looks like this:\n");
      for (j=0;j<=customnumcards;j++)
        printf("%s\n", customdeck[j].name);
      printf("Commander - %s\n", customcomm.name);
      printf("Nofcards = %d\n", customnumcards);
      if (disablelogs[0] == 'y')
        printf("Testing is in progress...\n");
      dummy = battle();
    }
    }
  return 1;
}

//===================MAIN==================
int main()
{
  srand(time(0));
  int dummy;
  char tmp[30];
  char dud;
  int j, k, l;
  for (j=0;j<=1;j++)
    for(k=0;k<=1;k++)
      for(l=0;l<=T-1;l++)
    wins[j][k][l] = 0;
  testnumber = 0;
  strcpy(disablelogs, "NO WAI\0");
  printf("Welcome to TyrantSim 1.0 alpha (CopyLeft qlzqlzuup aka Bumc)!\n\n");
  printf("Currently TyrantSim has %d test decks\n", T); 
  printf("Enter \'s\' for testing with sample decks and \'m\' to test against mission decks>  ");
  fgets(whichtest, S*sizeof(char), stdin);
  printf("Do you want to skip all logs(y/n)?>  ");
  fgets(disablelogs, S*sizeof(char), stdin);
  //--------------IF YOU WANT TO TEST AI, UNCOMMENT NEXT 3 STRINGS!----------
  //printf("Enter AI strength(0 for no AI, random placing)>  ");//NO AI!1111
  //scanf("%d", &AIstr);
  //dummy = getchar();//ololobug
  dummy = init();
  if (whichtest[0] == 's')
    {
      printf("Enter 1 for attacking test and 0 for defending test>  ");
      scanf(" %d", &l);
      dummy = getchar();//ololobug
      testattack = l % 2;
    }
  if (disablelogs[0] != 'y')
    {
      printf ("To enter debug mode, type 'y'>  ");
      scanf(" %c", &DEBUG);
      dummy = getchar();//ololobug
    }
  printf("Enter number of rounds>  ");
  scanf (" %d",&M);
  dummy = getchar();//ololobug
  if (custom == 0 && whichtest[0] == 's')
    {
      printf("Select deck to test(0-%d currently)>  ", T-1);
      scanf("%d", &decknumber);
      dummy = getchar();//ololobug
    }
  if (disablelogs[0] == 'y')
    printf("Testing is in progress...\n");
  dummy = battle();
  dummy = getchar();
  return 1;
}
